package artid.com.arttogoa;

import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import artid.com.arttogoa.R;

import artid.com.arttogoa.adapter.FeedBackAdapter;
import artid.com.arttogoa.barcode.Cache;
import artid.com.arttogoa.parser.AppItem;
import artid.com.arttogoa.parser.FeedbackListParser;
import artid.com.arttogoa.util.VioceUtils;
import artid.com.arttogoa.volley.VolleyBasic;
import artid.com.arttogoa.volley.VolleyCallback;
import artid.com.arttogoa.volley.VolleyData;
import artid.com.arttogoa.volley.VolleyMap;
import com.android.volley.VolleyError;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

/**
 * 
 *    
 * 项目名称：ArtworkArtA   
 * 类名称：FeedBackActivity   
 * 类描述：问题反馈   
 * 创建人：耿卫斌  
 * 创建时间：2016年5月16日 下午2:20:17      
 *
 */
public class FeedBackActivity extends BaseActivity implements OnClickListener{
	private EditText mEditText;
	private ImageView iv_vioce;
	private TextView subMit;
	private ListView mListView;
	private List<FeedbackListParser> list;
	private FeedBackAdapter adapter;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_feedback);
		initView();
	}

	private void initView(){
		setContent(this, getIntent());
		list=new ArrayList<FeedbackListParser>();
		mTittle = (TextView) findViewById(R.id.title_text);
		mTittle.setText("用户名");
		setBack();
		mEditText=(EditText) findViewById(R.id.FeedBack_EditText);
		iv_vioce=(ImageView) findViewById(R.id.iv_vioce);
		iv_vioce.setOnClickListener(this);
		subMit=(TextView) findViewById(R.id.FeedBack_subMit);
		subMit.setOnClickListener(this);
		mListView=(ListView) findViewById(R.id.FeedBack_ListView);
		getFeedBackList();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.FeedBack_subMit:
			//提交反馈意见
			if(!StringUtils.isEmpty(mEditText.getText().toString())){
				postFeedBack();
			}else{
				showToast("请填写您要反馈的内容");
			}
			break;
		case R.id.iv_vioce:
			VioceUtils vu = new VioceUtils(FeedBackActivity.this, iv_vioce, mEditText);
			vu.init();
			break;
		default:
			break;
		}
	}

	/**
	 * 提交反馈意见请求
	 */
	private void postFeedBack(){
		VolleyBasic.getInstance(FeedBackActivity.this).post(Cache.addfeedback,
				VolleyMap.getInstance().addfeedback(mEditText.getText().toString()), AppItem.class,new VolleyCallback() {
			public void onSuccess(VolleyData vd) {
				// TODO Auto-generated method stub
				System.out.println(vd.getJson());
				finish();
			}
			public void onError(VolleyError volleyError) {

			}
		});
	}
	
	/**
	 * 获取问题反馈列表
	 */
	private void getFeedBackList(){
		VolleyBasic.getInstance(FeedBackActivity.this).post(Cache.feedbackList,
				VolleyMap.getInstance().feedbackList(),FeedbackListParser.class,new VolleyCallback() {
			@SuppressWarnings("unchecked")
			public void onSuccess(VolleyData vd) {
				// TODO Auto-generated method stub
				System.out.println(vd.getJson());
				list=(List<FeedbackListParser>) vd.getList();
				System.out.println(list.toString());
				
			}
			public void onError(VolleyError volleyError) {

			}
		});
	}

	/**
	 * 动态设置ListView高度
	 * @param listView
	 */
	public void  setListViewHeightBasedOnChildren(ListView listView) {
		ListAdapter listAdapter = listView.getAdapter(); 
		if (listAdapter == null) {
			return;
		}

		int totalHeight = 0;
		for (int i = 0; i < listAdapter.getCount(); i++) {
			View listItem = listAdapter.getView(i, null, listView);
			listItem.measure(0, 0);
			totalHeight += listItem.getMeasuredHeight();
		}

		ViewGroup.LayoutParams params = listView.getLayoutParams();
		params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
		listView.setLayoutParams(params);
	}

}
